# SPDX-License-Identifier: GPL-3.0-or-later
#!/bin/sh
set -e

# apt换源
sudo cp sources.list /etc/apt/
sudo cp ros-latest.list /etc/apt/sources.list.d/
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt update -y
sudo apt upgrade -y
sudo apt autoremove --purge -y

# 安装ROS、N卡驱动、Qt、CUDA
sudo apt install build-essential git curl python3-pip ros-melodic-desktop-full python-rosdep python-rosinstall python-rosinstall-generator python-wstool python3-colcon-common-extensions python3-vcstool qt5-default qtcreator `nvidia-detector` cuda-10-0 -y

# 添加CUDA到系统路径
echo "export PATH=/usr/local/cuda/bin:$PATH" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc

# 默认初始化ROS
source /opt/ros/melodic/setup.bash
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc

# 手动模拟 rosdep init
sudo mkdir -p /etc/ros/rosdep/sources.list.d/
sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list https://mirrors.tuna.tsinghua.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list

# 为 rosdep update 换源
export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml
rosdep update

# 每次 rosdep update 之前，均需要增加该环境变量
# 为了持久化该设定，可以将其写入 .bashrc 中，例如
echo 'export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml' >> ~/.bashrc

# pip换源
python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pip
python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# git代理
git config --global user.name ubuntu
git config --global user.email ubuntu
git config --global init.defaultBranch main
# if you already knew how to use git and have a local git account, comment 3 lines above
git config --global url."https://ghproxy.com/https://github.com/".insteadOf "https://github.com"

# 清理缓存
sudo apt clean -y
python3 -m pip cache purge
